package com.taoge.p4.common.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.taoge.p4.common.constants.ErrorCode;
import lombok.Data;

@Data
public class ResponseData<T> {
    private int code;
    private String msg;
    private T data;

    public ResponseData() {
    }

    public ResponseData(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseData(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResponseData<T> success() {
        return new ResponseData<>(ErrorCode.SUCCESS_CODE, ErrorCode.SUCCESS_MSG);
    }

    public static <T> ResponseData<T> success(String msg) {
        return new ResponseData<>(ErrorCode.SUCCESS_CODE, msg);
    }

    public static <T> ResponseData<T> success(String msg, T data) {
        return new ResponseData<>(ErrorCode.SUCCESS_CODE, msg, data);
    }

    public static <T> ResponseData<T> error(String msg) {
        return new ResponseData<>(ErrorCode.ERROR_CODE, msg);
    }

    public static <T> ResponseData<T> error(int code, String msg) {
        return new ResponseData<>(code, msg);
    }

    public static <T> ResponseData<T> paramValidateError(String msg) {
        return new ResponseData<>(ErrorCode.PARAM_ERROR_CODE, msg);
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.code == ErrorCode.SUCCESS_CODE;
    }

}
